package com.shop.service;

import java.util.List;

import com.shop.dao.ProductDao;
import com.shop.domain.Order;
import com.shop.domain.PageBean;
import com.shop.domain.Product;

public class ProductService {
	private final int count = 10;

	public void addProduct(Product product) {
		ProductDao dao = new ProductDao();
		dao.insert(product);
	}

	public List<Product> findAll() {
		List<Product> products = new ProductDao().findAll();
		return products;
	}

	public Product findProduct(String id) {
		return new ProductDao().findProduct(id);
	}

	public List<Product> findAll(int pageNo) {
		int start = (pageNo - 1) * count;
		return new ProductDao().findAll(start, count);
	}

	public PageBean findByPage(int curPageNo) {
		ProductDao dao = new ProductDao();
		PageBean pageBean = new PageBean();
		pageBean.setPageNo(curPageNo);
		int start = (curPageNo - 1) * PageBean.COUNT;
		List<Product> products = dao.findAll(start, PageBean.COUNT);
		pageBean.setProducts(products);
		int totalNum = dao.getTotalNum();
		pageBean.setTotalNum(totalNum);
		pageBean.setTotalPage((totalNum + PageBean.COUNT - 1) / PageBean.COUNT);
		return pageBean;
	}

}
